﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace iPackTools
{
	public partial class Form1 : Form
	{
		public Form1()
		{
			InitializeComponent();
		}

		//-------------------------------------------------------------------
		//合并文件

		OpenFileDialog m_open; 
		List<string> m_file_list = new List<string>();
		List<Size> m_size_list = new List<Size>();
		private void but_file_Click(object sender, EventArgs e)
		{
			if (m_open == null)
			{
				m_open = new OpenFileDialog();
				m_open.InitialDirectory = Directory.GetCurrentDirectory();
				m_open.Multiselect = true;
			}

			m_open.Filter = "PGN文件|*.png";
			if (m_open.ShowDialog() == DialogResult.OK)
			{
				m_file_list.AddRange(m_open.FileNames);
				update_src_flie_list();
			}
		}
		private void update_src_flie_list()
		{
			list_src.Items.Clear();
			
			for(int i = 0; i < m_file_list.Count; i++)
			{
				string[] item_text = new string[2];
				item_text[0] = i.ToString();
				item_text[1] = Path.GetFileName(m_file_list[i]);
				ListViewItem item = new ListViewItem(item_text);
				list_src.Items.Add(item);
			}
		}

		SaveFileDialog m_save;
		private void but_combin_Click(object sender, EventArgs e)
		{
			int total_with = 0;
			int total_hight = 0;

			List<Image> list_img = new List<Image>();
			List<Size> list_size = new List<Size>();
			foreach (string file in m_file_list)
			{
				Image img = Image.FromFile(file);
				list_img.Add(img);

				total_hight += img.Height;
				if (total_with < img.Width) total_with = img.Width;
				list_size.Add(new Size(img.Width, img.Height));
			}

			//合并文件
			Bitmap bitmap = new Bitmap(total_with, total_hight);
			int heigh_offset = 0;
			foreach (Image img in list_img)
			{
				Bitmap sub = new Bitmap(img);
				for (int h = 0; h < img.Height; h++)
				{
					for (int w = 0; w < img.Width; w++)
					{
						bitmap.SetPixel(w, heigh_offset + h, sub.GetPixel(w, h));
					}
				}
				heigh_offset += sub.Height;
			}

			//保存合并后的文件 及合并信息
			if (m_save == null)
			{
				m_save = new SaveFileDialog();
				m_save.InitialDirectory = Directory.GetCurrentDirectory();
			}

			m_save.Filter = "PGN文件|*.png";
			m_save.FileName = Directory.GetCurrentDirectory() + "\\All_Combined.png";
			if (m_save.ShowDialog() == DialogResult.OK)
			{
				bitmap.Save(m_save.FileName);
				m_size_list = list_size;
			}
		}
		private void list_src_SelectedIndexChanged(object sender, EventArgs e)
		{
			if (list_src.SelectedItems.Count > 0)
			{
				int index = int.Parse(list_src.SelectedItems[0].Text);
				pic_src.Image = Image.FromFile(m_file_list[index]);
			}
		}

		private void pic_src_DoubleClick(object sender, EventArgs e)
		{
			if (pic_src.SizeMode == PictureBoxSizeMode.Zoom)
			{
				pic_src.SizeMode = PictureBoxSizeMode.Normal;
			}
			else
			{
				pic_src.SizeMode = PictureBoxSizeMode.Zoom;
			}
		}

		//-------------------------------------------------------------------
		//CQ减色

		private void button6_Click(object sender, EventArgs e)
		{
			System.Diagnostics.ProcessStartInfo Info = new System.Diagnostics.ProcessStartInfo();
			//设置外部程序名
			Info.FileName = "cq.exe";
			//设置外部程序的启动参数（命令行参数）为test.txt
			//Info.Arguments = m_save == null ? "" : m_save.FileName;
			//设置外部程序工作目录为C:\
			Info.WorkingDirectory = Directory.GetCurrentDirectory();
			//声明一个程序类
			System.Diagnostics.Process Proc;
			try
			{//启动外部程序//
				Proc = System.Diagnostics.Process.Start(Info);
			}
			catch
			{
				MessageBox.Show("找不到cq.exe, 请拷贝到当前目录下!", "错误", MessageBoxButtons.OK, MessageBoxIcon.Error);
				return;
			}
		}

		private void but_dis_Click(object sender, EventArgs e)
		{
			if (m_open == null)
			{
				m_open = new OpenFileDialog();
				m_open.InitialDirectory = Directory.GetCurrentDirectory();
			}

			m_open.Filter = "CQ输出的图像|*.png";
			if (m_open.ShowDialog() == DialogResult.OK)
			{
				but_dis.Tag = m_open.FileNames;
				but_dis.Text = Path.GetFileName(m_open.FileName);
			}
		}

		private void but_color_Click(object sender, EventArgs e)
		{
			if (m_open == null)
			{
				m_open = new OpenFileDialog();
				m_open.InitialDirectory = Directory.GetCurrentDirectory();
			}
	
			m_open.Filter = "CQ输出的色板|*.pal";
			if (m_open.ShowDialog() == DialogResult.OK)
			{
				but_color.Tag = m_open.FileNames;
				but_color.Text = Path.GetFileName(m_open.FileName);
			}
		}

		//-------------------------------------------------------------------
		//转成bin 及 lzss压缩
		private void but_pack_Click(object sender, EventArgs e)
		{

		}

		//-------------------------------------------------------------------------------------
		//-------------------------------------------------------------------------------------

		private List<Bitmap> split_cq_output_file(string file, List<Size> list_size)
		{
			Bitmap img_src = new Bitmap(Bitmap.FromFile(file)); //要分割的图像
			List<Bitmap> list = new List<Bitmap>(); //分割的小图像列表

			int offset_y = 0;
			foreach (Size item in list_size)
			{
				Bitmap item_img = new Bitmap(item.Width, item.Height);
				for (int y = 0; y < item.Height; y++)
				{
					for (int x = 0; x < item.Width; x++)
					{
						Color color = img_src.GetPixel(x, offset_y + y);
						item_img.SetPixel(x, y, color);
					}
				}

				list.Add(item_img);
				offset_y += item.Height;
			}
			return list;
		}



		//根据图像列表 及色表,生成bin表
		private List<byte[]> Create_Bin(string bin_file, List<Bitmap> list_img, List<Color> pal)
		{
			byte[] bin = new byte[1024 * 1024];
			List<byte[]> list_bin = new List<byte[]>();
			foreach (Bitmap item in list_img)
			{
				for (int y = 0; y < item.Height; y++)
				{
					for (int x = 0; x < item.Width; x++)
					{
					//    int index = pal
					//    bin[y*item.Height + x] = 
					}
				}
			}

			return null;
		}
	}
}
